<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时语音识别</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }

        h1 {
            margin-bottom: 20px;
        }

        #results {
            width: 90%;
            max-width: 600px;
            height: 300px;
            overflow-y: auto;
            border: 1px solid #ccc;
            background: #fff;
            padding: 10px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .status {
            margin-top: 10px;
            font-size: 14px;
        }

        button {
            margin-top: 10px;
            padding: 8px 12px;
            font-size: 14px;
            border: none;
            background-color: #007bff;
            color: white;
            border-radius: 4px;
            cursor: pointer;
        }

        button:disabled {
            background-color: #999;
            cursor: not-allowed;
        }
    </style>
</head>
<body>
    <h1>实时语音识别结果</h1>
    <div id="results"></div>
    <div class="status" id="status">状态：未连接</div>
    <button id="clearButton">清空结果</button>

    <script>
        const resultsDiv = document.getElementById("results");
        const statusDiv = document.getElementById("status");
        const clearButton = document.getElementById("clearButton");

        let ws;

        function connectWebSocket() {
            ws = new WebSocket("ws://localhost:9000/ws2");

            ws.onopen = () => {
                statusDiv.textContent = "状态：已连接";
                statusDiv.style.color = "green";
            };

            ws.onmessage = (event) => {
                try {
                    // 将接收到的数据解析为 JSON 对象
                    const data = JSON.parse(event.data);
                    const timestamp = new Date().toLocaleTimeString();

                    // 在页面中显示提取的字段和时间
                    const newResult = document.createElement("div");
                    newResult.textContent = `时间: ${timestamp} ${data.event}: ${data.text} `;
                    resultsDiv.appendChild(newResult);

                    // 滚动到底部
                    resultsDiv.scrollTop = resultsDiv.scrollHeight;
                } catch (error) {
                    console.error("解析 WebSocket 数据时出错:", error);
                }
            };

            ws.onclose = () => {
                statusDiv.textContent = "状态：断开连接";
                statusDiv.style.color = "red";

                // 尝试重新连接
                setTimeout(connectWebSocket, 3000);
            };

            ws.onerror = () => {
                console.error("WebSocket 出现错误！");
                ws.close();
            };
        }

        // 初始化 WebSocket
        connectWebSocket();

        // 清空按钮逻辑
        clearButton.addEventListener("click", () => {
            resultsDiv.innerHTML = "";
        });
    </script>
</body>
</html>
